package org.budo.geo.location.service.api;

import java.util.Map;

import org.budo.ip.location.service.api.util.AdministrativeDivisionUtil;
import org.budo.support.lang.util.StringUtil;

import lombok.Getter;
import lombok.Setter;

/**
 * BudoGeoLocation的基本实现
 * 
 * @author lmw
 */
@Getter
@Setter
public class AbstractBudoGeoLocation implements BudoGeoLocation {
    /**
     * 国家
     */
    private String country;

    /**
     * 省份
     */
    private String province;

    /**
     * 城市
     */
    private String city;

    @Override
    public Integer getProvinceCode() {
        String province = this.getProvince();

        if (StringUtil.isEmpty(province)) {
            return null;
        }

        Map<String, Integer> provinceMap = AdministrativeDivisionUtil.getProvinceMap();
        return provinceMap.get(province);
    }

    @Override
    public Integer getCityCode() {
        String city = this.getCity();
        if (StringUtil.isEmpty(city)) {
            return null;
        }

        Integer provinceCode = this.getProvinceCode();
        if (null == provinceCode) {
            return null;
        }

        Map<String, Integer> cityMap = AdministrativeDivisionUtil.getCityMap(provinceCode);
        return cityMap.get(city);
    }

    @Override
    public String toString() {
        return super.toString() + ", " + this.toText();
    }

    @Override
    public String toText() {
        String text = this.getCountry();

        if (!StringUtil.isEmpty(this.getProvince())) {
            text += this.getProvince();
        }

        if (!StringUtil.isEmpty(this.getCity())) {
            text += this.getCity();
        }

        return text;
    }
}